/*
 * UMediaFormat.cpp
 *
 *  Created on: 2020年3月26日
 *      Author: chuanjiang.zh
 */

#include "UMediaFormat.h"
#include <string>


class MediaFormatPrivate
{
public:
    std::string m_videoProp;
	std::string m_audioConfig;

	MediaFormatPrivate():
		m_videoProp(),
		m_audioConfig()
	{
		m_videoProp.reserve(1024);
		m_audioConfig.reserve(128);
	}

};

UMediaFormat::UMediaFormat():
		m_duration(),
		m_codec(UMEDIA_CODEC_NONE),
		m_width(),
		m_height(),
		m_framerate(),
		m_bitrate(),
		m_profile(),
		m_level(),
		m_gop(),
		m_clockRate(),
		m_audioCodec(UMEDIA_CODEC_NONE),
		m_audioProfile(),
		m_channels(0),
		m_sampleRate(0),
		m_sampleBits(16),
		m_frameSize(1024),
		m_audioBitrate(),
		m_audioRate(),
		m_subtitle(),
		m_videoDuration(),
		m_pimpl(new MediaFormatPrivate())
{

}

UMediaFormat::~UMediaFormat()
{
	delete m_pimpl;
}

UMediaFormat::UMediaFormat(const UMediaFormat& fmt):
		m_duration(),
		m_codec(UMEDIA_CODEC_NONE),
		m_width(),
		m_height(),
		m_framerate(),
		m_bitrate(),
		m_profile(),
		m_level(),
		m_gop(),
		m_clockRate(),
		m_audioCodec(UMEDIA_CODEC_NONE),
		m_audioProfile(),
		m_channels(0),
		m_sampleRate(0),
		m_sampleBits(16),
		m_frameSize(1024),
		m_audioBitrate(),
		m_audioRate(),
		m_subtitle(),
		m_pimpl(new MediaFormatPrivate())
{
    assign(fmt);
}

UMediaFormat& UMediaFormat::operator = (const UMediaFormat& fmt)
{
	return assign(fmt);
}

UMediaFormat& UMediaFormat::assign(const UMediaFormat& fmt)
{
	m_duration = fmt.m_duration;
	m_codec = fmt.m_codec;
	m_width = fmt.m_width;
	m_height = fmt.m_height;
	m_framerate = fmt.m_framerate;
	m_bitrate = fmt.m_bitrate;
	m_profile = fmt.m_profile;
	m_level = fmt.m_level;
	m_gop = fmt.m_gop;
	m_clockRate = fmt.m_clockRate;

	m_audioCodec = fmt.m_audioCodec;
	m_audioProfile = fmt.m_audioProfile;
	m_channels = fmt.m_channels;
	m_sampleRate = fmt.m_sampleRate;
	m_sampleBits = fmt.m_sampleBits;
	m_frameSize = fmt.m_frameSize;
	m_audioBitrate = fmt.m_audioBitrate;
	m_audioRate = fmt.m_audioRate;

	m_subtitle = fmt.m_subtitle;

	*m_pimpl = *fmt.m_pimpl;

	return *this;
}

size_t UMediaFormat::getVideoPropSize() const
{
	return m_pimpl->m_videoProp.size();
}

const uint8_t* UMediaFormat::getVideoProp() const
{
	return (const uint8_t*)m_pimpl->m_videoProp.c_str();
}

size_t UMediaFormat::getAudioConfigSize() const
{
	return m_pimpl->m_audioConfig.size();
}

const uint8_t* UMediaFormat::getAudioConfig() const
{
	return (const uint8_t*)m_pimpl->m_audioConfig.c_str();
}

void UMediaFormat::setVideoProp(const uint8_t* data, size_t length)
{
	m_pimpl->m_videoProp.assign((const char*)data, length);
}

void UMediaFormat::setVideoProp(const char* data, size_t length)
{
	m_pimpl->m_videoProp.assign(data, length);
}

void UMediaFormat::setAudioConfig(const uint8_t* data, size_t length)
{
	m_pimpl->m_audioConfig.assign((const char*)data, length);
}

void UMediaFormat::setAudioConfig(const char* data, size_t length)
{
	m_pimpl->m_audioConfig.assign(data, length);
}

void UMediaFormat::clear()
{
	m_duration = 0;
	m_codec = UMEDIA_CODEC_NONE;
	m_width = 0;
	m_height = 0;
	m_framerate = 0;
	m_bitrate = 0;
	m_profile = 0;
	m_level = 0;
	m_gop = 0;
	m_clockRate = 0;

	m_audioCodec = UMEDIA_CODEC_NONE;
	m_audioProfile = 0;
	m_channels = 0;
	m_sampleRate = 0;
	m_sampleBits = 16;
	m_frameSize = 1024;
	m_audioBitrate = 0;
	m_audioRate = 0;

	m_subtitle = 0;

	m_pimpl->m_videoProp.clear();
	m_pimpl->m_audioConfig.clear();
}

void UMediaFormat::copyVideo(const UMediaFormat& fmt)
{
	m_duration = fmt.m_duration;

	m_codec = fmt.m_codec;
	m_width = fmt.m_width;
	m_height = fmt.m_height;
	m_profile = fmt.m_profile;
	m_level = fmt.m_level;
	m_gop = fmt.m_gop;
	m_framerate = fmt.m_framerate;
	m_bitrate = fmt.m_bitrate;
	m_clockRate = fmt.m_clockRate;

	m_pimpl->m_videoProp = fmt.m_pimpl->m_videoProp;
}

void UMediaFormat::copyAudio(const UMediaFormat& fmt)
{
	m_audioCodec = fmt.m_audioCodec;
	m_channels = fmt.m_channels;
	m_sampleRate = fmt.m_sampleRate;
	m_sampleBits = fmt.m_sampleBits;
	m_audioProfile = fmt.m_audioProfile;
	m_audioBitrate = fmt.m_audioBitrate;
	m_frameSize = fmt.m_frameSize;
	m_audioRate = fmt.m_audioRate;

	m_subtitle = fmt.m_subtitle;

	m_pimpl->m_audioConfig = fmt.m_pimpl->m_audioConfig;
}

void UMediaFormat::clearVideoProp()
{
	m_pimpl->m_videoProp.clear();
}

void UMediaFormat::clearAudioConfig()
{
	m_pimpl->m_audioConfig.clear();
}